package org.apache.lucene.demo;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.io.*;

import org.apache.lucene.document.Field.TermVector;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.FilterIndexReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.SegmentReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.TermFreqVector;
import org.apache.lucene.index.TermPositions;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class MyIndexReaderExample
{
	public static void main(String[] args) throws Exception, IOException
	{
		IndexReader indexReader = FilterIndexReader.open(FSDirectory.open(new File("index0")));
		
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in, "GBK"));
		
		Term t = new Term("answer", "阴道");
		TermDocs tDocs = indexReader.termDocs(t);
		int totalDocN = indexReader.docFreq(t);
		System.out.println(t.text() + " 出现在：");
		while (tDocs.next())
		{
			int docID = tDocs.doc();
			System.err.print(docID + " ");
		}
		System.err.println("");
		System.out.println("一共 ： " + totalDocN + "次.");
		
		System.out.println("请输入需要查看的文档编号");

		TermPositions termPositions = indexReader.termPositions(t);
		for (int i = 0; i < 10; i ++)
		{
			termPositions.next();
		}
		int i = 0;
		System.out.println("第" + termPositions.doc()+ " 篇文档: " +t.text() + "出现在下面的位置：");
		while (i < termPositions.freq())
		{
			
			System.err.println(termPositions.nextPosition() + " ");
			i ++;
		}
		
		System.err.println(" ");
		
	}
}